﻿using System;
using System.Collections.Generic;
using System.Text;
using AgencyManagerSystem.Model;
using AgencyManagerSystem.DataUpdater;
using AgencyManagerSystem.Common;
using AgencyManagerSystem.DataFetcher;
using System.Reflection;
using System.Linq;

namespace AgencyManagerSystem.Core
{

    public class OrderUpdaterCreator : MainDetailUpdaterCreator<OrderModel, OrderDetailModel>
    {
        OrderFetcher orderFetcher = new OrderFetcher();
        OrderDetailFetcher orderDetailFetcher = new OrderDetailFetcher();
        public OrderUpdater CreateOrderUpdaterInstance()
        {
            var mainUpdater = OrderUpdater.GetMainUpdater();
            mainUpdater.DataUpdating += FlagAndApproveFlagValid;
            mainUpdater.DataUpdating += ReferExist;
            mainUpdater.DataUpdating += StatusValid;
            mainUpdater.DataDeleting += StatusDeleteValid;
            OrderUpdater odUpdater = new OrderUpdater(orderFetcher, orderDetailFetcher, mainUpdater);
            odUpdater.DataBulkUpdating += ProductionValid;
            odUpdater.DataBulkUpdating += CommonValid;
            odUpdater.DataBulkDeleting += StatusBulkDeleteValid;
            return odUpdater;
        }

        private void StatusBulkDeleteValid(MainDetailUpdater<OrderModel, OrderDetailModel> sender, DataBulkDeletingEventArgs<OrderModel, OrderDetailModel> e)
        {
            if (e.Delete == null) return;
            if (e.Delete.Status != (int)StatusCode.Open||e.Delete.ApproveFlag == 1)
            {
                e.Message = String.Format("单据{0}不是开立状态不能删除", e.Delete.Code);
                e.Cancel = true;
                return;
            }
        }

        private void StatusDeleteValid(BaseUpdater<OrderModel> sender, DataDeletingEventArgs<OrderModel> e)
        {
            if (e.Delete == null) return;
            if (e.Delete.Status != (int)StatusCode.Open || e.Delete.ApproveFlag == 1)
            {
                e.Message = String.Format("单据{0}不是开立状态不能删除", e.Delete.Code);
                e.Cancel = true;
                return;
            }
        }

        private void ProductionValid(MainDetailUpdater<OrderModel, OrderDetailModel> sender, DataBulkUpdatingEventArgs<OrderModel, OrderDetailModel> e)
        {
            if (e.DetailInsert == null)
                return;
            var productionFetcher = new ProductionFetcher();
            foreach (var item in e.DetailInsert)
            {
                var production = productionFetcher.Get(item.ProductionId);
                if (production == null)
                {
                    e.Cancel = true;
                    e.Message = String.Format("找不到物料ID={0}的资料", item.ProductionId);
                    return;
                }
                string message = ValidDimension(item, production);
                if (!String.IsNullOrWhiteSpace(message))
                {
                    e.Cancel = true;
                    e.Message = message;
                    return;
                }
                if (String.IsNullOrWhiteSpace(item.ProductionCode) || String.IsNullOrWhiteSpace(item.ProductionName) || String.IsNullOrWhiteSpace(item.Unit))
                {
                    e.Cancel = true;
                    e.Message = String.Format("物料{0}的品名，品号，单位不能为空", item.ProductionId);
                    return;
                }
                if (item.Qty <= 0)
                {
                    e.Cancel = true;
                    e.Message = String.Format("物料{0}的数量必须大于0", item.ProductionId);
                    return;
                }
            }
        }
        private string ValidDimension(OrderDetailModel item, ProductionModel production)
        {
            var dimensionDocFetcher = new DimensionDocFetcher();
            for (int i = 1; i <= 5; i++)
            {
                //PropertyInfo prodDimension = typeof(ProductionModel).GetProperty("Dimension" + i);
                //if ((bool)prodDimension.GetValue(production))
                //{
                //    PropertyInfo itemDimension = typeof(OrderDetailModel).GetProperty("Dimension" + i);
                //    int dimensionInItem = (int)itemDimension.GetValue(item);
                //    var docs = dimensionDocFetcher.ListDimensionByProduction(production.AutoId, i);
                //    var records = from dimension in docs where dimension.AutoId == dimensionInItem select dimension;
                //    if (records.Count() == 0)
                //    {
                //        return String.Format("物料{0}的纬度{2}选项{1}不存在", item.ProductionId, dimensionInItem, i);
                //    }
                //}
                //else if (item.Dimension1 != 0)
                //{
                //    return String.Format("物料{0}的纬度{1}不启用，不允许录入资料", item.ProductionId, i);
                //}
            }
            
            return "";
        }
        private void StatusValid(BaseUpdater<OrderModel> sender, DataUpdatingEventArgs<OrderModel> e)
        {
            if (e.Insert == null)
                return;
            if (e.Insert.ApproveFlag != 0)
            {
                e.Cancel = true;
                e.Message = String.Format("单据{0}不是开立状态不能修改", e.Insert.Code);
                return;
            }
            if(e.Insert.Status == (int)StatusCode.Closed&&String.IsNullOrWhiteSpace( e.Insert.CloseReason))
            {
                e.Cancel = true;
                e.Message = String.Format("单据{0}是关闭状态下必须指定关闭原因", e.Insert.Code);
                return;
            }
        }

        private void ReferExist(BaseUpdater<OrderModel> sender, DataUpdatingEventArgs<OrderModel> e)
        {
            if (e.Insert == null)
                return;
            var retailFetcher = new RetailFetcher();
            var retail = retailFetcher.Get(e.Insert.Retail);
            if (retail == null)
            {
                e.Cancel = true;
                e.Message = String.Format("无法找到经销商{0}对应的档案", e.Insert.Retail);
                return;
            }
            if (e.Insert.Sales != 0)
            {
                var salesFetcher = new UserFetcher();
                var sales = salesFetcher.Get(e.Insert.Sales);
                if (sales == null)
                {
                    e.Cancel = true;
                    e.Message = String.Format("无法找到业务员{0}对应的档案", e.Insert.Sales);
                    return;
                }
            }
            if (String.IsNullOrWhiteSpace(e.Insert.Code))
            {
                e.Cancel = true;
                e.Message = "编码不能为空";
                return;
            }
            DateTime dt;
            if (!DateTime.TryParse(e.Insert.DocDate, out dt))
            {
                e.Cancel = true;
                e.Message = String.Format("{0}不是正确的日期格式", e.Insert.DocDate);
                return;
            }
        }
    }
}